﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Interface;
using DAL.Models;

namespace DAL.Dao
{
    public class DeliverymanDaoimpl : BaseDao, IDeliveryDao
    {
        public int ExistNum { get { using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions)) return context.Deliveryman.Count(); } private set { } }
        public DeliverymanDaoimpl(bool use_clock = true) : base(use_clock)
        {
            
        }
        public IList<Deliveryman> GetList()
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return  (from i in context.Deliveryman orderby i.Id select i).ToList();
        }

        public Deliveryman? GetItemByID(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return (from i in context.Deliveryman
                    where i.Id==id
                    select i).ToList().FirstOrDefault();
        }
        public (int,IList<Deliveryman>) GetList(int rows,int page)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                var rs = (from i in context.Deliveryman orderby i.Id select i).ToList();
                return (GetTotalPages(rs.Count, rows), rs.Skip((page - 1) * rows).Take(rows).ToList());
            }
               
        }


        public IList<Deliveryman> GetDeliverymanInfo(string username)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                //使用Lambda表达式查询数据
                var info = (from i in context.Deliveryman
                            where i.UserName == username
                            select i).ToList();

                return info;
            }
              
        }


        public bool Delete(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                lock (clock_obj_delete)
                {
                    Deliveryman? deliveryman = context.Deliveryman.Find(id);
                    if (deliveryman == null) return false;
                    context.Deliveryman.Remove(deliveryman);
                    context.SaveChanges();
                    return true;
                }
            }
               

        }
        public IList<Deliveryman> GetDeliverymanInfo(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                //使用Lambda表达式查询数据
                var info = (from i in context.Deliveryman
                            where i.Id == id
                            select i).ToList();

                return info;
            }

        }

        public Deliveryman? Find(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return context.Deliveryman.Find(id);
        }
        public bool Add(Deliveryman deliveryman)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                lock (clock_obj_add)
                {

                    var res = (from i in context.Deliveryman where i.UserName.Equals(deliveryman.UserName) select i).FirstOrDefault();
                    if (res != null) return false;
                    //deliveryman.Id = GetList().Last().Id;
                    context.Deliveryman.Add(deliveryman);
                    context.SaveChanges();
                    return true;
                }
            }

        }



        public Deliveryman? Login(string username, string userpass)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return  (from i in context.Deliveryman
                                      where i.UserName.Trim().Equals(username) && i.UserPass.Trim().Equals(userpass)
                                  select i).ToList().FirstOrDefault();
       
        }
      

        public bool Update(int id, Deliveryman newitem)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                lock (clock_obj_update)
                {

                    Deliveryman? item = (from i in context.Deliveryman
                                         where i.Id == id
                                         select i).ToList().FirstOrDefault();
                    if (item == null)
                    {
                        return false;
                    }
                    //DbEntityEntry<Bag> entry = context.Entry<Bag>(newitem);
                    //3.设置该对象为被修改过
                    //entry.State = EntityState.Unchanged;
                    //4.设置该对象的 uName属性为修改状态，同时 entry.State 被修改为 Modified 状态
                    item.UserName = newitem.UserName;
                    item.UserPass = newitem.UserPass;
                    item.TrueName = newitem.TrueName;
                    item.Phone = newitem.Phone;
                    item.Idcard = newitem.Idcard;
                    item.UpdateTime = newitem.UpdateTime;


                    context.SaveChanges();

                    return true;
                }
            }
               

        }





    }
}
